package com.nineclock.sign.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.common.utils.DateTimeUtil;
import com.nineclock.sign.dto.SignDTO;
import com.nineclock.sign.mapper.SignClockInMapper;
import com.nineclock.sign.mapper.SignPictureMapper;
import com.nineclock.sign.pojo.SignInfo;
import com.nineclock.sign.pojo.SignPicture;
import com.nineclock.sign.service.SignClockINService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class SignClockINServiceImpl implements SignClockINService {

    @Autowired
    SignClockInMapper signClockInMapper;
    @Autowired
    SignPictureMapper signPictureMapper;




    @Override
    public void singClockInInsert(SignDTO signDTO) {
        //健壮性判断
        if (signDTO == null) {
            throw new NcException(ResponseEnum.ERROR);
        }
        //从线程获取当前公司id 用户id
        Long companyId = CurrentUserHolder.get().getCompanyId();
        Long companyUserId = CurrentUserHolder.get().getCompanyUserId();
        String images = signDTO.getImages();


        SignInfo signInfo = BeanHelper.copyProperties(signDTO, SignInfo.class);

        signInfo.setCompanyId(companyId);
        signInfo.setCompanyUserId(companyUserId);

        //签到时间,创建时间
        Date date = new Date();
        signInfo.setCreateTime(date);
        //签到日期
        String date2 = DateTimeUtil.dateToStr(date, DateTimeUtil.TIME_FORMAT_2);
        signInfo.setCreateDate(date2);
        //签到月份
        String date1 = DateTimeUtil.dateToStr(date, DateTimeUtil.TIME_FORMAT_4);
        signInfo.setCreateMonth(date1);

        //图片存储位置
        signClockInMapper.insert(signInfo);

        String[] split = images.split(",");
        List<String> list = Arrays.asList(split);
        if (CollectionUtil.isNotEmpty(list)) {
            for (String datUrl : list) {
                SignPicture signPicture = new SignPicture();
                signPicture.setUrl(datUrl);
                signPicture.setSignId(signInfo.getId());
                signPictureMapper.insert(signPicture);
            }





        }
    }
    //查看所有员工本月签到记录
    @Override
    public List<SignDTO> SignCurrentUserByMonth(String sting) {

     //查询当前月份的签到
        //创建查询条件构造器
     LambdaQueryWrapper<SignInfo> signInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
     signInfoLambdaQueryWrapper.eq(SignInfo::getCreateMonth,sting);
     signInfoLambdaQueryWrapper.eq(SignInfo::getCompanyId,CurrentUserHolder.get().getCompanyId());
        signInfoLambdaQueryWrapper.eq(SignInfo::getCompanyUserId,CurrentUserHolder.get().getCompanyUserId());

        //查询员工列表
        List<SignInfo> signInfolist=signClockInMapper.selectList(signInfoLambdaQueryWrapper);
     //判断
        if (signInfolist.isEmpty()){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        };


     //查询员工图片位置
     LambdaQueryWrapper<SignPicture> signPictureLambdaQueryWrapper=new LambdaQueryWrapper<>();

        List<SignDTO> signDTOS = new ArrayList<>();

        for (SignInfo signInfo : signInfolist) {
            Long id = signInfo.getId();
            signPictureLambdaQueryWrapper.eq(SignPicture::getSignId, id);

           SignPicture signPicture = signPictureMapper.selectOne(signPictureLambdaQueryWrapper);
         if (signPicture==null){
         SignDTO signDTO = BeanHelper.copyProperties(signInfo, SignDTO.class);
         signDTOS.add(signDTO);

          }else {
            String url = signPicture.getUrl();
            List<String> urll = Arrays.asList(url.split(","));
            SignDTO signDTO = BeanHelper.copyProperties(signInfo, SignDTO.class);
            signDTO.setImages(url);
            signDTO.setSignPictureUrlList(urll);
            signDTOS.add(signDTO);}

        }
        return signDTOS;



    }
}
